<?php
abstract class AbstractFetcher {
	/**
	 * @var string
	 */
	protected $url;
	
	/**
	 * @var string
	 */
	private $service;
	
	/**
	 * @var string (e.g. youtube-ID)
	 */
	protected $contentInformation;
	
	/**
	 * @var string (e.g. funnycats)
	 */
	protected $title;
	
	/**
	 * @var string (e.g. some funny cats, just look)
	 */
	protected $description;
	
	/**
	 * @var array
	 */
	protected $previewImages = array();
	
	/**
	 * @var bool|FetcherGeneric
	 */
	private $fallbackFetcher = false;
	
	/**
	 * constructor
	 * @param string $url
	 * @return AbstractFetcher
	 */
	public function __construct($url){
		$this->url = $url;
		$classname = get_class($this);
		$this->service = substr($classname, 0, -7);
		if($this->setContentInformation()){
			$this->setTitle();
			$this->setDescription();
			$this->setPreviewImages();
		}
		elseif(!$this->fallbackFetcher){
			require_once('Fetchers/GenericFetcher.php');
			$this->fallbackFetcher = new GenericFetcher($url);
		}
	}
	
	/**
	 * returns the service name, its same as classname till Fetcher (e.g. Youtube)
	 * @return string
	 */
	public function getService(){
		if($this->fallbackFetcher){
			return $this->fallbackFetcher->getService();
		}
		else{
			return $this->service;
		}
	}
	
	/**
	 * returns the content informations, if found (e.g. the youtube-ID)
	 * @return string
	 */
	public function getContentInformation(){
		if($this->fallbackFetcher){
			return $this->fallbackFetcher->getContentInformation();
		}
		else{
			return $this->contentInformation;
		}
	}
	
	/**
	 * returns the title of the page/video/picture
	 * @return string
	 */
	public function getTitle(){
		if($this->fallbackFetcher){
			return $this->fallbackFetcher->getTitle();
		}
		else{
			return $this->title;
		}
	}
	
	/**
	 * returns the description of the video/picture/page
	 * @return string
	 */
	public function getDescription(){
		if($this->fallbackFetcher){
			return $this->fallbackFetcher->getDescription();
		}
		else{
			return $this->description;
		}
	}
	
	/**
	 * returns an array of previewimageurls
	 * @return array
	 */
	public function getPreviewImages(){
		if($this->fallbackFetcher){
			return $this->fallbackFetcher->getPreviewImages();
		}
		else{
			return $this->previewImages;
		}
	}
	
	/**
	 * sets the contentInformation, each Fetcher has its own rules for this, generic fetcher doesnt do that
	 * if this returns false, theres an error in url or fetcher, so we use genericfetcher as fallback
	 * @return bool
	 */
	abstract protected function setContentInformation();
	
	/**
	 * sets the title of the item, each fetcher does this his own way
	 * @return void
	 */
	abstract protected function setTitle();
	
	/**
	 * sets the description of the item, each fetcher does this his own way
	 * @return void
	 */
	abstract protected function setDescription();
	
	/**
	 * sets the preview images
	 * @return void
	 */
	abstract protected function setPreviewImages();
}